import { defHttp } from '/@/common/util/axios';
import { message } from 'ant-design-vue';
import { useGlobSetting } from '/@/hooks/setting';
const globSetting = useGlobSetting();
const baseUploadUrl = globSetting.uploadUrl;
enum Api {
  fieldCheck = '/sys/common/fieldCheck',    //唯一性校验

  getPostPage = '/sys/post/getPage',
  userList = '/sys/user/getPage',
  roleList = '/sys/role/getPage',

  getDeptById = '/sys/sysDept/getId',
  getDeptTreeSync = '/sys/sysDept/getDeptTreeSync',
  getDeptTree = '/sys/sysDept/getDeptTree',
  queryTreeList = '/sys/sysDept/getDeptTree',

  getDeptTreeByIds = '/sys/sysDept/getDeptTreeVoByIds',
  getMsgSend = '/sys/msg/getMsgSend',                     //消息详情
  getMessagePage = '/sys/msg/getMessagePage',                         //消息列表-分页
  updateReadStatusMessage = '/sys/msg/updateReadStatusMessage',       //消息已读

  getDictItemsApi = '/sys/dict/getDictItems/',
  getTableList = '/sys/user/getPage',

  getPluraList = '/emp/common/getPluraList', // 班组下拉框options
  getBaseRelList = '/emp/common/getBaseRelList', // 人员下拉框options
  getMonitoringStatus = '/emp/hubTeamMonitoring/getMonitoringStatus',  //监控转态：查询是否可编辑修改
  getPersonTypeListByType = '/emp/pnelCommon/getPersonTypeList',
  getPeriodTree = '/emp/epmBasePeriod/periodTree', //考核周期树
  getSuitUnitOrgApi = '/emp/common/findTree', // 上级部门树
}

/**
 * url地址抛出
 */
export const getPostPageApi = Api.getPostPage;

/**
 * 唯一校验
 * @param params
 */
export const fieldCheck = (params) => defHttp.get({ url: Api.fieldCheck, params }, { isTransformResponse: false });



export const getMsgSend = (id) => defHttp.get({ url: Api.getMsgSend, params: { id } });
export const getMessagePage = (params?) => defHttp.get({ url: Api.getMessagePage, params });
export const updateReadStatusMessage = (params) => defHttp.post({ url: Api.updateReadStatusMessage, params });

export const getPeriodByParams = (params) => defHttp.post({ url: Api.getPeriodTree, params }, { joinParamsToUrl: true });
/**
 * 获取周期树
 * @param params
 */
export const getPeriodTree = () => {
  const params = {
    type: 'yearM',
  };
  return new Promise((resolve) => {
    defHttp.post({ url: Api.getPeriodTree, params }, { joinParamsToUrl: true }).then((res) => {
      res.forEach((item) => {
        item.disabled = true;
      });
      resolve(res);
    });
  });
};

export const getPeriodTreeAll = () => {
  const params = {
    type: 'yearM',
  };
  return new Promise((resolve) => {
    defHttp.post({ url: Api.getPeriodTree, params }, { joinParamsToUrl: true }).then((res) => {
      resolve(res);
    });
  });
};

export const getPeriodTreeByYear = () => {
  const params = {
    type: 'year',
  };
  return new Promise((resolve) => {
    defHttp.post({ url: Api.getPeriodTree, params }, { joinParamsToUrl: true }).then((res) => {
      resolve(res);
    });
  });
};
export const getPeriodTreeByQuarter = () => {
  const params = {
    type: 'yearQuarter',
  };
  return new Promise((resolve) => {
    defHttp.post({ url: Api.getPeriodTree, params }, { joinParamsToUrl: true }).then((res) => {
      res.forEach((item) => {
        item.disabled = true;
      });
      resolve(res);
    });
  });
};
/**
 * 获取班组搜索下拉框
 */
export const getPluraList = () => {
  return new Promise((resolve) => {
    defHttp.get({ url: Api.getPluraList }).then((res) => {
      if (res && res.length > 0) {
        res.forEach((item) => {
          item.label = item.orgName;
          item.value = item.orgCode;
        });
      }
      resolve(res);
    });
  });
};

export const getBaseRelList = (params) => defHttp.get({ url: Api.getBaseRelList, params });
export const getMonitoringStatus = (params) => defHttp.post({ url: Api.getMonitoringStatus, params }, { isTransformResponse: false });
export const getPersonTypeListByType = () => defHttp.get({ url: Api.getPersonTypeListByType });


/**
 * 查询示例详情
 * @param params
 */
export const getDeptById = (params) => defHttp.get({ url: Api.getDeptById, params });

export const getSuitUnitOrg = () => {
  return new Promise((resolve) => {
    defHttp.get({ url: Api.getSuitUnitOrgApi }).then((res) => {
      resolve(res);
    });
  });
};

/**
 * 上级部门树
 * @param params
 */
export const selectSuperDepartTreeList = (params?) => {
  const titleMap = (arr = []) => {
    arr.forEach((i: any) => {
      i.title = i.orgName;
      i.value = i.deptId + '';
      i.id = i.deptId;
      i.key = i.deptId + '';
      if (i.children?.length) {
        titleMap(i.children);
      }
    });
  };
  return new Promise((resolve, reject) => {
    defHttp.get({ url: Api.getSuitUnitOrgApi, params }).then((res) => {
      titleMap(res);
      resolve(res);
    });
  });
  // return defHttp.get({ url: Api.selectSuperDepartTreeList, params });
};

/**
 * 上传父路径
 */
export const uploadUrl = `${baseUploadUrl}/sys/common/upload`;

/**
 * 职务列表
 * @param params
 */
export const getPostPage = (params) => defHttp.get({ url: Api.getPostPage, params });

/**
 * 用户列表
 * @param params
 */
export const getUserList = (params) => defHttp.get({ url: Api.userList, params });

/**
 * 角色列表
 * @param params
 */
export const getRoleList = (params) => defHttp.get({ url: Api.roleList, params });

/**
 * 获取部门树列表
*/
export const getDeptTree = (params?) => defHttp.get({ url: Api.getDeptTree, params });
export const getDeptTreeSync = (params?) => defHttp.get({ url: Api.getDeptTreeSync, params });
export const getDeptTreeByIds = (params?) => defHttp.get({ url: Api.getDeptTreeByIds, params });
export const queryTreeList = (params?) =>  defHttp.get({ url: Api.queryTreeList, params });

/**
 * 根据字典code加载字典text
 */
export const getDictItems4Code = (dictCode) =>  defHttp.get({ url: Api.getDictItemsApi + dictCode }, { joinTime: false });
export const getDictItems4Obj = (params) =>  defHttp.get({ url: Api.getDictItemsApi + params.code }, { joinTime: false });
/**
 * 部门用户modal选择列表加载list
 */
export const getTableList = (params) => defHttp.get({ url: Api.getTableList, params });

/**
 * 文件上传
 */
export const uploadFile = (params, success) => defHttp.uploadFile({ url: uploadUrl }, params, { success });
/**
 * 下载文件
 * @param url 文件路径
 * @param fileName 文件名
 * @param parameter
 * @returns {*}
 */
export const downloadFile = (url, fileName?, parameter?) => {
  return getFileblob(url, parameter).then((data) => {
    if (!data || data.size === 0) {
      message.warning('文件下载失败');
      return;
    }
    if (typeof window.navigator.msSaveBlob !== 'undefined') {
      window.navigator.msSaveBlob(new Blob([data]), fileName);
    } else {
      let url = window.URL.createObjectURL(new Blob([data]));
      let link = document.createElement('a');
      link.style.display = 'none';
      link.href = url;
      link.setAttribute('download', fileName);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
};

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export const getFileblob = (url, parameter) => {
  return defHttp.get(
    {
      url: url,
      params: parameter,
      responseType: 'blob',
    },
    { isTransformResponse: false }
  );
};

/**
 * 【用于评论功能】自定义文件上传-方法
 */
export const uploadMyFile = (url, data) => defHttp.uploadMyFile(url, data);
